using System;
using System.Web;
using System.Xml;
using System.Xml.Xsl;

using umlungu.engineX.emailX;

/*	-----------------------------------------------------------------------	
	Copyright:	umlungu consulting (pty) ltd
	Author:		Alan Benington	
	Started:	2006-03-08	
	Status:		release	
	Version:	2.0.0
	Build:		20071123
	License:	GNU General Public License
	-----------------------------------------------------------------------	*/

/*	---------------------------------------------------------------------------	
	Development Notes:
	==================
	20070524:	Modification for .Net 2.0
	20071123:	Replaced x_email with EmailX.x_email
	---------------------------------------------------------------------------	*/

namespace umlungu.engineX.blogX {
	/// <summary>
	/// x_broker class.
	/// <para>This class is a 'broker' for a request on a site using the displayX class to display a page</para>
	/// <para>Note the following:</para>
	/// <para>- Respond to a request and process the Form or Querystring parameters of the request</para>
	/// <para>- They get passed the page itself (the displayX object) and can access the page properties/methods</para>
	/// <para>By default this class provides:</para>
	/// <para>- protected methods to get request parameters</para>
	/// <para>- protected methods to get xsl template(s)</para>
	/// </summary>
	public class x_broker : x_actor {
		#region Invisible properties
		private const string error_form_send = "Form email sending error: ";
		private const string error_content_spam = "Content contains non-permitted words";
		private string spam_list = "http|[url|mailto|fuck";
		#endregion

		#region Visible properties
		#endregion

		#region Constructors/Destructors
		/// <overloads>Constructor</overloads>
		/// <summary>Default constructor</summary>
		/// <param name="thispage">The web application Page object</param>
		public x_broker(displayX thispage) : base(thispage) {
		}
		#endregion

		#region Public methods
		///	<overloads>Broker processing method</overloads>
		/// <summary>
		/// Does the broker processing of the page request. 
		///	This method is typically overriden in derived classes in order to respond to the request before displaying the page.
		///	Example: this method can be overridden by a method that reads a form and submits values to the back-end.
		/// </summary>
		public virtual void Process() {
			_SendForm();
		}
		/// <param name="flag">Some process flag</param>
		public virtual void Process(bool flag) {
			Process();
		}
		/// <param name="value">Some string value</param>
		public virtual void Process(string value) {
			Process();
		}
		/// <param name="thispage">The web application Page object</param>
		public virtual void Process(displayX thispage) {
			UIPage = thispage;
			Process();
		}
		/// <overloads>
		/// Looks for web spam content and removes
		///	- Expand as necassary
		///	- Similar to isSpam (private) in displayX.aspx
		/// </overloads>
		/// <summary>
		///	specified spam word list
		/// </summary>
		public bool SpamCheck(string checkList, string input, bool raiseError) {
			bool retval = false;
			try {
				string test = input.ToLower();
				string delimStr = "|";
			    char [] delimiter = delimStr.ToCharArray();
				string [] split = null;
				split = checkList.Split(delimiter);
				foreach (string s in split) {
					if (test.IndexOf(s) >= 0) {
						retval = true;
						break;
					}
	            }
			} catch (Exception e) {
				throw(new x_exception("error_content_spam", String.Concat(e.Message, " : ", e.StackTrace)));
			}
			
			if (raiseError && retval)
				throw(new x_exception("error_content_spam", error_content_spam));
			return retval;
		}
		/// <summary>
		///	default spam word list
		/// </summary>
		public bool SpamCheck(string input, bool raiseError) {
			return SpamCheck(spam_list, input, raiseError);
		}
		/// <summary>Checks for spam identified by the length of the input string and the absence of word breaks</summary>
		public bool SpamCheck(string input, int length, int threshhold, bool raiseError) {
			bool retval = false;
			if (input.Length > length)
				retval = true;
			else if (input.Length < threshhold)
				retval = false;
			else {
				int space = input.IndexOf(' ');
				if (space == -1 || space > threshhold)
					retval = true;
			}
			if (raiseError && retval)
				throw(new x_exception("error_content_spam", error_content_spam));
			return retval;
		}
		#endregion
		
		#region Protected methods
		///	<overloads>Emails a submitted form</overloads>
		/// <summary>To the configured email address</summary>
		protected void _SendForm() {
			try {
				x_email thisemail = new x_email();
				/* html...*/
				thisemail.Type = x_emailtype.html;
				thisemail.Send(UIPage.Request, _GetTemplate("emailX.xsl"));
				/* xml or text...
				thisemail.Type = x_emailtype.xml;
				thisemail.Send(UIPage.Request);
				*/
				/* text...
				thisemail.Message = String.Concat("\nType: ", feedbackScore, "\nTitle: ", contactTitle, "\nName: ", contactName, "\nEmail: ", contactEmail, "\nTelephone: ", contactTelno, "\nCellphone: ", contactCellno, "\nQuery: ", feedbackText);
				thisemail.Send();
				*/
			}
			catch(System.Exception e) {
				throw(new x_exception("error_form_send", String.Concat(error_form_send, e.Message)));
			}
		}
		/// <summary>To the configured email address with subject line</summary>
		///	<param name="subj">The subject line of the email</param>
		protected void _SendForm(string subj) {
			try {
				x_email thisemail = new x_email();
				thisemail.Subject = subj;
				thisemail.Type = x_emailtype.html;
				thisemail.Send(UIPage.Request, _GetTemplate("emailX.xsl"));
			}
			catch(System.Exception e) {
				throw(new x_exception("error_form_send", String.Concat(error_form_send, e.Message)));
			}
		}
		/// <summary>To the configured email address with subject line</summary>
		///	<param name="subj">The subject line of the email</param>
		///	<param name="bcc">The bcc address of the email</param>
		protected void _SendForm(string subj, string bcc) {
			try {
				x_email thisemail = new x_email();
				thisemail.Subject = subj;
				thisemail.Bcc = bcc;
				thisemail.Type = x_emailtype.html;
				thisemail.Send(UIPage.Request, _GetTemplate("emailX.xsl"));
			}
			catch(System.Exception e) {
				throw(new x_exception("error_form_send", String.Concat(error_form_send, e.Message)));
			}
		}
		#endregion
				
		#region Private methods
		#endregion
	}
}
